package com.mb.app.controller;

import com.mb.app.domain.TbAppInfo;
import com.mb.app.service.ITbAppInfoService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @author zwl
 * @program
 * @description 应用管理
 * @date 2019-03-21 16:58
 **/
@Controller
@RequestMapping("/app/apply")
@Slf4j
public class ApplyController extends BaseController {
    
    @Autowired
    private ITbAppInfoService appInfoService;

    @GetMapping("/index")
    String index(){
        return "/app/apply/apply_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbAppInfo> list = appInfoService.queryList(query);
        int total = appInfoService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(){
        return "/app/apply/apply_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbAppInfo tbAppInfo, HttpServletRequest request){
        tbAppInfo.setCreateUserId(getUserId());
        int result = appInfoService.save(tbAppInfo);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("appId") Long appId, HttpServletRequest request){
        int result = appInfoService.remove(appId);
        return R.ok();
    }

    @GetMapping("/dtl/{appId}")
    String dtl(@PathVariable("appId") Long appId, Model model){
        model.addAttribute("appInfo",appInfoService.getDtl(appId));
        return "/app/apply/apply_info";
    }


    @GetMapping("/edit/{appId}")
    String edit(@PathVariable("appId") Long appId, Model model){
        model.addAttribute("appInfo",appInfoService.getDtl(appId));
        return "/app/apply/apply_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbAppInfo tbAppInfo, HttpServletRequest request){
        tbAppInfo.setUpdateUserId(getUserId());
        int result = appInfoService.update(tbAppInfo);
        return R.ok();
    }

    /**
     * 应用名称、应用代码是否已存在
     */
    @PostMapping("/appNameCodeExit")
    @ResponseBody
    boolean appNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !appInfoService.appNameCodeExit(params);
    }

}


